package com.siwei.mes.service.laboratory.factory.material.fly;

import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.common.util.NullUtils;
import com.siwei.mes.entity.experiment.CheckBatchConfig;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentDetail;
import com.siwei.mes.entity.experiment.flyAsk.FlyAskHdzs;
import com.siwei.mes.entity.laboratory.DataDefaultUc;
import com.siwei.mes.entity.system.TestProjectInfo;
import com.siwei.mes.entity.system.TestProjectSpecConfig;
import com.siwei.mes.enums.TestProjectEnum;
import com.siwei.mes.mapper.laboratory.UploadLaboratoryMapper;
import com.siwei.mes.service.laboratory.factory.LaboratoryAbstractService;
import com.siwei.mes.service.laboratory.factory.entity.LaboratoryDownloadVo;
import com.siwei.mes.service.laboratory.factory.material.MaterialLaboratoryFactory;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 粉煤灰-活性指数
 *
 * @Description: 粉煤灰-活性指数
 * @Version: 1.0
 * @author: mlchen
 * @date: 2025/3/29 17:42
 */
@Service
public class FlyHxzsFactory extends LaboratoryAbstractService implements MaterialLaboratoryFactory {
    private static final Logger log = LoggerFactory.getLogger("laboratory");

    @Resource
    private UploadLaboratoryMapper uploadLaboratoryMapper;

    private final TestProjectEnum testProjectEnum = TestProjectEnum.FLY_ASK_PARAM_FMH_HXZS;

    @Override
    public void processUpload(JSONObject objJson, String sampleNo, Experiment experiment, List<CheckBatchConfig> batchConfigList, List<TestProjectInfo> testProjectInfoList, List<TestProjectSpecConfig> testProjectSpecConfigList, ExperimentDetail detail, String samId) {
        log.info("开始上传到协会数据库：【粉煤灰-{}】", testProjectEnum.getName());
        FlyAskHdzs flyAskHdzs = objJson.toJavaObject(FlyAskHdzs.class);
        if (flyAskHdzs == null) {
            log.info("粉煤灰-【{}】数据为空，跳过上传！objJson：{}", testProjectEnum.getName(), objJson);
            return;
        }

        if (flyAskHdzs.getHxzsInfoList() == null || flyAskHdzs.getHxzsInfoList().isEmpty()) {
            log.info("粉煤灰-【{}】活性指数数据为空，跳过上传！objJson：{}", testProjectEnum.getName(), objJson);
            return;
        }

        if (StringUtils.isBlank(flyAskHdzs.getHxzsInfoList().get(0).getHxzs())) {
            log.info("粉煤灰-【{}】第一条数据的活性指数数据为空，跳过上传！objJson：{}", testProjectEnum.getName(), objJson);
            return;
        }

        //存储协会需要做的项目集合
        Set<String> set = new HashSet<>();

        for (TestProjectInfo testProjectInfo : testProjectInfoList) {
            if (detail.getTestProjectCode() != null && testProjectInfo.getTestCode() != null && testProjectInfo.getTestCode().equals(detail.getTestProjectCode())) {
                if (testProjectInfo.getParameterName() != null && !testProjectInfo.getParameterName().isEmpty()) {
                    //添加进协会需要做的项目集合
                    set.addAll(Arrays.asList(testProjectInfo.getParameterName().split(",")));
                }
            }
        }

        for (String str : set) {
            DataDefaultUc defaultUc = new DataDefaultUc();

            Map<String, Object> itemParameter = uploadLaboratoryMapper.findItemParameter(str, samId);

            if (itemParameter == null || itemParameter.get("ParameterID") == null) {
                log.error("未查询到对应的材料：value = " + str + "SampleID =" + samId);
                continue;
            }
            String parameterID = itemParameter.get("ParameterID").toString();

            String jcrq = flyAskHdzs.getHxzsInfoList().get(0).getJcrq();

            defaultUc.setConclusion(flyAskHdzs.getHxzsInfoList().get(0).getDxjl());
            defaultUc.setUcResultValue(flyAskHdzs.getHxzsInfoList().get(0).getDxjl());
            defaultUc.setUcTestValue(flyAskHdzs.getHxzsInfoList().get(0).getHxzs());

            //获取标准值
            for (CheckBatchConfig projectInfo : batchConfigList) {
                if (projectInfo.getTestProjectCode() != null && projectInfo.getTestProjectCode().equals(TestProjectEnum.FLY_ASK_PARAM_FMH_HXZS.getCode())) {
                    defaultUc.setUcUnit(NullUtils.ifNull(projectInfo.getCheckValueUnit()));
                }
            }


            //根据物料信息设置判断配置
            for (TestProjectSpecConfig test : testProjectSpecConfigList) {
                if (TestProjectEnum.FLY_ASK_PARAM_FMH_HXZS.getCode().equals(test.getTestProjectCode())) {
                    if (experiment.getMaterialsName() != null && experiment.getMaterialsSpecs() != null && experiment.getMaterialsName().equals(test.getMaterialsName()) && experiment.getMaterialsSpecs().equals(test.getMaterialsSpec())) {
                        String json = test.getObjJson();
                        super.setUcStandardValue(defaultUc, json, "hxzs7d");
                    }
                }
            }

            //赋值
            super.setDefUc(defaultUc, experiment, "成型室", sampleNo, jcrq, detail);

            defaultUc.setUcParameterID(parameterID);//项目id

            //新增或修改DataDefaultUc
            super.insertOrUpdateDataDefaultUc(sampleNo, parameterID, defaultUc);

        }

    }

    @Override
    public LaboratoryDownloadVo processDownload(String sampleNo, Experiment experiment, ExperimentDetail detail, Map<String, String> parameterMap, String samId, List<TestProjectInfo> testProjectInfoList) {
        log.info("开始从协会数据库下载数据：【粉煤灰-{}】", testProjectEnum.getName());
        log.info("暂不需要下载！");
        return null;
    }
}
